package com.study.hot.jetpack.workmanager

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.work.Constraints
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import com.study.hot.databinding.ActivityWorkManagerBinding
import com.study.hot.jetpack.workmanager.work.MyWork
import org.jetbrains.anko.sdk27.coroutines.onClick

class WorkMangerActivity : AppCompatActivity() {

    private val workManager = WorkManager.getInstance(this)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityWorkManagerBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.btnStart.onClick {
            oneTimeWork()
        }
    }

    private fun oneTimeWork() {
        val constraints = Constraints.Builder()
            .setRequiredNetworkType(NetworkType.CONNECTED)
            .build()
        val workRequest = OneTimeWorkRequestBuilder<MyWork>()
            .setConstraints(constraints) // 添加约束条件
            .build()
        workManager.enqueue(workRequest)
    }

}